package com.wgworkshop.pems.controller;

import com.wgworkshop.pems.checkgroup.CreateCheckGroup;
import com.wgworkshop.pems.checkgroup.PatchCheckGroup;
import com.wgworkshop.pems.checkgroup.UpdateCheckGroup;
import com.wgworkshop.pems.common.ResponseData;
import com.wgworkshop.pems.model.AttendanceModel;
import com.wgworkshop.pems.model.AttendanceView;
import com.wgworkshop.pems.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 考勤控制器。
 * Created by wlc on 2017/7/9.
 */
@RestController
@RequestMapping("/attendances")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    @PostMapping
    ResponseData<AttendanceModel> create(@RequestBody @Validated(CreateCheckGroup.class) AttendanceModel attendanceModel) {
        AttendanceModel resultModel = attendanceService.save(attendanceModel);
        return new ResponseData<AttendanceModel>().success(resultModel);
    }

    @DeleteMapping("/{id}")
    ResponseData<Void> delete(@PathVariable("id") Long id) {
        attendanceService.delete(id);
        return new ResponseData<Void>().success();
    }

    @PatchMapping("/{id}")
    ResponseData<AttendanceModel> update(@PathVariable("id") Long id,
                                         @RequestBody @Validated(PatchCheckGroup.class) AttendanceModel attendanceModel) {
        AttendanceModel resultModel = attendanceService.update(id, attendanceModel);
        return new ResponseData<AttendanceModel>().success(resultModel);
    }

    @PutMapping
    ResponseData<AttendanceModel> update(@RequestBody @Validated(UpdateCheckGroup.class) AttendanceModel attendanceModel) {
        AttendanceModel resultModel = attendanceService.update(attendanceModel);
        return new ResponseData<AttendanceModel>().success(resultModel);
    }

    @GetMapping("/{id}")
    ResponseData<AttendanceModel> findOne(@PathVariable("id") Long id) {
        AttendanceModel attendanceModel = attendanceService.findOne(id);
        return new ResponseData<AttendanceModel>().success(attendanceModel);
    }

    @GetMapping("/week/{week}/num/{num}")
    ResponseData<AttendanceView> searchByWeekAndClassNum(@PathVariable("week") Integer week,
                                                         @PathVariable("num") Integer num,
                                                         @RequestParam(value = "shift", required = false, defaultValue = "0") Integer shift) {
        AttendanceView view = attendanceService.getAttendanceView(week, num, shift);
        return new ResponseData<AttendanceView>().success(view);
    }
}
